package com.twohand.admin.Service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.twohand.admin.Dto.RoleListQueryDto;
import com.twohand.admin.Entity.Menu;
import com.twohand.admin.Entity.Role;
import com.twohand.admin.Mapper.RoleMapper;
import com.twohand.admin.Service.MenuService;
import com.twohand.admin.Service.RoleService;
import com.twohand.admin.Util.PageResponse;
import com.twohand.admin.Vo.RolePageListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    MenuService menuService;
    @Override
    public List<Role> getRoleList() {
        return roleMapper.selectList(null);
    }

    @Override
    public RolePageListVo getRoleList(RoleListQueryDto roleListQueryDto) {
        //构建分页
        PageHelper.startPage(roleListQueryDto.getPageNum(),roleListQueryDto.getPageSize());
        List<Role> roleList = roleMapper.selectList(null);
        PageInfo<Role> pageInfo = new PageInfo<>(roleList);

        //获取最全的baseMenu 用于权限管理
        List<Menu> menuList = menuService.getBaseMenu();
        return new RolePageListVo(pageInfo,roleList,menuList);
    }

    @Override
    public Boolean addRole(Role role) {
        return roleMapper.insert(role) > 0;
    }

    @Override
    public Boolean updateRole(Role role) {
        return roleMapper.updateById(role) > 0;
    }

    @Override
    public Boolean deleteRole(Integer id) {
        return roleMapper.deleteById(id) > 0;
    }

    @Override
    public List<Integer> getRoleMenu(Integer id) {
        return roleMapper.getRoleMenuId(id);
    }
}
